package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions;

import java.util.ArrayList;
import java.util.Collection;

import vg.services.data_base_manager.data.graph.Edge;
import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.data_base_manager.data.header.GraphHeader;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.main_manager.MainManager;

/**
 * Creates new visualization for new graph.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class NewGraphAction extends DefaultActionAdapter {
	private Graph graph; 	

	/**
	 * Creates new graph view from a graph.
	 */
	public NewGraphAction(Graph graph) {
		super("New graph");
		this.graph = graph;
	}
	
	@Override
	public void action(final IGraphRenderer graphRenderer) {
        try {
            // remove old graph
            graphRenderer.resetGraph();

            // load graph
            if (graph != null) {
                graphRenderer.setDataBaseGraphId(graph.getStorableId());
                graphRenderer.setDirected(graph.isDirected());

                GraphHeader graph_header = MainManager.graphDataBase.getGraphHeader(graph.getStorableId());
                if (graph_header != null) {
                    graphRenderer.setDataBaseGraphModelId(graph_header.getGraphModelId());
                }

                long all_element_size = 0;
                long curr_element_size = 0;

                Collection<Vertex> vertices = graph.getVertices();
                Collection<Edge> edges = graph.getEdges();

                if (vertices != null) all_element_size += vertices.size();
                if (edges != null) all_element_size += edges.size();

                if (vertices != null) {
                    Collection<Vertex> tmp_vertices = new ArrayList<Vertex>();
                    for (Vertex buff : vertices) {
                        tmp_vertices.add(buff);
                        curr_element_size++;
                        if (curr_element_size % 100 == 0) {
                            graphRenderer.addVertices(tmp_vertices);
                            tmp_vertices.clear();
                            progress.setValue((long)((curr_element_size / (double)all_element_size) * 100));
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException ex) {
                                MainManager.logger.printException(ex);
                            }
                        }
                    }
                    graphRenderer.addVertices(tmp_vertices);
                    tmp_vertices.clear();
                }

                if (edges != null) {
                    Collection<Edge> tmp_edges = new ArrayList<Edge>();
                    for (Edge buff : edges) {
                        tmp_edges.add(buff);
                        curr_element_size++;
                        if (curr_element_size % 100 == 0) {
                            graphRenderer.addEdges(tmp_edges);
                            tmp_edges.clear();
                            progress.setValue((long)((curr_element_size / (double)all_element_size) * 100));
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException ex) {
                                MainManager.logger.printException(ex);
                            }
                        }
                    }
                    graphRenderer.addEdges(tmp_edges);
                    tmp_edges.clear();
                }

                graphRenderer.refreshView();
            }
        } catch (Throwable ex) {
            MainManager.logger.printException(ex);
        } finally {
            progress.finish();
        }
	}
}
